#include "stdio.h"
#include "stdlib.h"

int main()
{
	// Data descriptions.
///////////////////////////////////////////////////////
	const char *filename = "CT_CRANI.img";
	int width = 256, height = 256, depth = 223;
	double scalex = 0.7, scaley = 0.7, scalez = 2;
///////////////////////////////////////////////////////
	FILE *in;
	if( (in = fopen(filename, "rb") ) == NULL )
	{
		printf("cannot open infile");
		exit(-1);
	}

	const char *outfile = "output.txt";
	FILE *out;
	if( (out = fopen(outfile, "a")) == NULL )
	{
		printf("cannot open outfile\n");
		exit(-1);
	}

	fprintf(out, "%d 1 1 0 0\n", width*height*depth);

	printf("Saving coordinates...\n");
	int k = 1;
	double xc = 0, yc = 0, zc = 0;
	for(unsigned int z = 1; z <= depth; z++)
	{
		yc = 0;
		for(unsigned int y = 1; y <= height; y++)
		{
			xc = 0;
			for(unsigned int x = 1; x <= width; x++)
			{
				// Id
				fprintf(out, "%d ", k);

				// Coordinates
				fprintf(out, "%f %f %f", xc, yc, zc);

				// Return
				fprintf(out, "\n");
				
				xc += scalex;
				k++;
			}
			yc += scaley;
		}
		zc += scalez;
	}
	fprintf(out, "1 1 hex 1 2 3 4 5 6 7 8\n");
	fprintf(out, "1 1\n");
	fprintf(out, "no,\n");

	unsigned char data;
	printf("Saving data...\n");
	for(int i = 0; i < width*height*depth; i++)
	{
		fscanf(in, "%c", &data);
		fprintf(out, "%d %d\n", i+1, data);
	}
	fclose(out);
	return 0;
}
